<template>
	<view class="wrap">
		<view>
			<u-button @click="clearAll" type="primary">一键清空所有巷道</u-button>
		</view>
		<u-empty text="暂无数据" :show="isEmpty" mode="list"></u-empty>
		<u-swipe-action :show="item.show" :index="index" 
			v-for="(item, index) in list" :key="item.areaName" 
			@click="click" @open="open"
			:options="options"
		>
			<view class="u-border-bottom" style="margin-top: 20rpx;background: #e5e9f2;">
				<view class="u-demo-block__content u-border-bottom">
					<u-row
							justify="space-between"
							customStyle="margin-bottom: 10px"
					>
						<u-col
								span="6"
						>
							<view class="demo-layout">{{'巷道名称：' + item.areaName}}</view>
						</u-col>
						<u-col
								span="6"
						>
							<view class="demo-layout">{{'优先级：' + item.priority}}</view>
						</u-col>
					</u-row>
					<u-row
							justify="space-between"
							customStyle="margin-bottom: 10px"
					>
						<u-col
								span="6"
						>
							<view class="demo-layout">{{'储位数量：' + item.allNum}}</view>
						</u-col>
						<u-col
								span="6"
						>
							<view class="demo-layout">{{'已用数量：' + item.fullNum}}</view>
						</u-col>
					</u-row>
					<u-row
							justify="space-between"
							customStyle="margin-bottom: 10px"
					>
						<u-col
								span="6"
						>
							<view class="demo-layout">{{'锁定数量：' + item.lockNum}}</view>
						</u-col>
						<u-col
								span="6"
						>
							<view class="demo-layout">{{'空余数量：' + item.emptyNum}}</view>
						</u-col>
					</u-row>

				</view>
			</view>
		</u-swipe-action>
		<u-popup v-model="isPriority" mode="center" width="500rpx" height="400rpx" border-radius="10">
			<view class="u-text-center u-m-t-20">优先级</view>
			<view class=" u-m-20">
				<u-input v-model="priority.priority" type="digit" :border="true"
					placeholder="请输入优先级" />
			</view>
			<view style="height: 80rpx;text-align: center;">
				<view class="close-btn u-m-t-40">
					<u-button size="medium" @click="setPriority()">确定</u-button>
				</view>
			</view>
		</u-popup>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				priority:{
					target:"",
					priority:10
				},
				isEmpty:true,
				isPriority:false,
				list: [],
				disabled: false,
				btnWidth: 220,
				show: false,
				options: [
					{
						text: '优先级',
						style: {
							backgroundColor: '#007aff'
						}
					},
					{
						text: '清空',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			}
		},
		
		onShow() {
			this.getLists()
		},
		
		onPullDownRefresh() {
			this.getLists()
			uni.stopPullDownRefresh()
		},
		methods: {
			getLists() {
				this.list = [];
				this.isEmpty = true;
				this.$http.post('/rest/tuskAgvTaskController/getMapStorage', {}, {
					params: {
						// taskNo: this.keyword,
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if (res.statusCode == 200) {
						if(res.data.code == 200){
							this.list = res.data.data;
							if(this.list.length > 0 ){
								this.isEmpty = false;
							}
						}else{
							this.$refs.uToast.show({
								title: res.data.msg,
								duration: 2000,
								type: 'error'
							})
						}
					} else {
						this.$refs.uToast.show({
							title: res.data.msg,
							duration: 2000,
							type: 'error'
						})
					}
				})
			},
			click(index, index1) {
				if(index1 == 0) {
					//设置优先级
					this.isPriority = true;
					this.priority.target = this.list[index].areaName;
				} else if(index1 == 1) {
					uni.showModal({
						title:'确认清空通道吗?',
						success: (res) => {
							if (res.confirm) {
								//清空通道
								this.$http.post('/rest/tuskAgvTaskController/clear', {}, {
									params: {
										areaName: this.list[index].areaName
									},
									header: {
										'Authorization': uni.getStorageSync('token'),
									}
								}).then(res => {
									if (res.statusCode == 200) {
										if(res.data.code == 200){
											this.$refs.uToast.show({
												title: '操作成功',
												duration: 3000,
												type: 'success'
											})
											this.getLists()
											this.isPriority = false
										}else{
											this.$refs.uToast.show({
												title: res.data.msg,
												duration: 2000,
												type: 'error'
											})
										}
									} else {
										this.$refs.uToast.show({
											title: res.data.msg,
											duration: 2000,
											type: 'error'
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				}
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.list[index].show = true;
				this.list.map((val, idx) => {
					if(index != idx) this.list[idx].show = false;
				})
			},
			setPriority(){
				this.$http.post('/rest/tuskAgvTaskController/setPriority', {}, {
					params: {
						target: this.priority.target,
						priority: this.priority.priority,
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					console.log(res)
					if (res.statusCode == 200) {
						if(res.data.code == 200){
							this.getLists()
							this.isPriority = false
						}else{
							this.$refs.uToast.show({
								title: res.data.msg,
								duration: 2000,
								type: 'error'
							})
						}
					} else {
						this.$refs.uToast.show({
							title: res.data.msg,
							duration: 2000,
							type: 'error'
						})
					}
				})
				
			},
			clearAll(){
				uni.showModal({
					title:'确认清空通道吗?',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '正在提交'
							})
							this.$http.post('/rest/tuskAgvTaskController/clearAll', {}, {
								params: {
								},
								header: {
									'Authorization': uni.getStorageSync('token'),
								}
							}).then(res => {
								uni.hideLoading()
								if (res.statusCode == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										duration: 3000,
										type: 'success'
									})
									this.getLists()
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	    .wrap {
	        padding: 30rpx;
	    }
		
		.demo-layout {
			height: 25px;
			border-radius: 4px;
		}
</style>
